using System;
using System.Windows.Input;

namespace WpfWordsLearner.Helper
{
	internal class BaseCommand : ICommand
	{
		private readonly Action _command;
		private readonly Func<bool> _canExecute;

		public BaseCommand(Action command, Func<bool> canExecute = null)
		{
			if (command == null)
				throw new ArgumentNullException("command");
			_canExecute = canExecute;
			_command = command;
		}

		public void Execute(object parameter)
		{
			_command();
		}

		public bool CanExecute(object parameter)
		{
			if (_canExecute == null)
				return true;
			return _canExecute();
		}

		public event EventHandler CanExecuteChanged;

		protected void OnCanExecuteChanged()
		{
			EventHandler handler = CanExecuteChanged;
			if (handler != null)
			{
				handler(this, EventArgs.Empty);
			}
		}
	}

	internal class BaseCommand<T> : ICommand
	{
		T m_data = default(T);
		private readonly Action<T> _command;
		private readonly Func<bool> _canExecute;

		public BaseCommand(Action<T> command, Func<bool> canExecute = null)
		{
			if (command == null)
				throw new ArgumentNullException("command");
			_canExecute = canExecute;
			_command = command;
		}

		public void Execute(object parameter)
		{
			m_data = (T)parameter;
			_command(m_data);
		}

		public bool CanExecute(object parameter)
		{
			if (_canExecute == null)
				return true;
			return _canExecute();
		}

		public event EventHandler CanExecuteChanged;

		protected void OnCanExecuteChanged()
		{
			EventHandler handler = CanExecuteChanged;
			if(handler!=null)
			{
				handler(this, EventArgs.Empty);
			}
		}
	}
}